package com.yueya.basic.feedback.action;

import java.util.HashMap;
import java.util.Map;

import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.yueya.basic.feedback.model.Feedback;
import com.yueya.common.action.BaseController;
import com.yueya.common.model.Json;

/**
 * 意见反馈
 * 
 * @author yangfeng
 * 
 */
public class FeedbackController extends BaseController {

	Map<String, Object> map = new HashMap<String, Object>();

	public void index() {
		render("/WEB-INF/views/basic/feedback/list.jsp");
	}

	public void getListPager() {
		map.clear();

		Page<Record> page = Feedback.service.pagination(getPage(), getRows(), map);
		jsonMap.put("total", page.getTotalRow());
		jsonMap.put("rows", page.getList());
		renderJson(JsonKit.toJson(jsonMap));
	}

	public void doAdd() {
		this.render("/WEB-INF/views/basic/feedback/edit.jsp");
	}

	public void doEdit() {
		String id = this.getPara("id");
		Feedback record = Feedback.dao.findById(id);
		setAttr("feedback", record);
		this.render("/WEB-INF/views/basic/feedback/edit.jsp");
	}
	
	public void view() {
		String id = this.getPara("id");
		Feedback record = Feedback.dao.findById(id);
		setAttr("feedback", record);
		this.render("/WEB-INF/views/basic/feedback/view.jsp");
	}

	public void save() {
		Record r = this.getUserWithCookie();

		Feedback model = getModel(Feedback.class);
		String content = this.getPara("content");
		if (content != null && !"".equals(content)) {
			model.set("content", content);
		}
		String userId = null;
		if (r != null) {
			userId = r.getStr("id");
			model.set("user_id", userId);
		}
		boolean bl = Feedback.service.save(model);

		Json j = new Json();
		if (bl) {
			j.setSuccess(true);
			j.setMsg("提交成功");
		} else {
			j.setSuccess(false);
			j.setMsg("提交失败");
		}

		this.renderJson(j);
	}

	public void update() {
		Feedback model = Feedback.dao.findById(getPara("id"));
		boolean bl = false;
		if (model != null) {
			if ("1".equals(model.getStr("status"))) {
				model.set("status", "0");
			} else if ("0".equals(model.getStr("status"))) {
				model.set("status", "1");
			}

			bl = model.update();
		}
		Json j = new Json();
		if (bl) {
			j.setSuccess(true);
			j.setMsg("删除成功!");
		} else {
			j.setSuccess(false);
			j.setMsg("删除失败!");
		}
		this.renderJson(j);
	}

	public void remove() {
		Feedback model = Feedback.dao.findById(getPara("id"));
		boolean bl = false;
		if (model != null) {
			model.set("del_flag", "1");
			bl = model.update();
		}
		Json j = new Json();
		if (bl) {
			j.setSuccess(true);
			j.setMsg("删除成功!");
		} else {
			j.setSuccess(false);
			j.setMsg("删除失败!");
		}
		this.renderJson(j);
	}

	public void bathremove() {
		Boolean bl = false;

		String idsStr = this.getPara("ids");

		if (idsStr != null) {
			String[] ids = idsStr.split(";");
			Feedback model = null;
			for (int i = 0; i < ids.length; i++) {

				if (!"".equals(ids[i]) && ids[i] != null) {
					model = Feedback.dao.findById(ids[i]);
					model.set("del_flag", 1);
					if (model.update()) {
						bl = true;
					} else {
						bl = false;
						break;
					}
				}

			}
		}

		Json j = new Json();
		if (bl == true) {
			j.setSuccess(true);
			j.setMsg("删除成功!");
		} else {
			j.setSuccess(false);
			j.setMsg("删除失败!");
		}
		this.renderJson(j);
	}

}
